﻿namespace BM.Tools.WebControls.MForm.Design
{
    using System.ComponentModel;
    using System.ComponentModel.Design;
    using System.Web.UI.Design;
    using System.Web.UI.Design.WebControls;

    using BM.Tools.WebControls.MForm.Controls;

    /// <summary>
    /// This is a <see cref="ControlDesigner"/> if the <see cref="OffspringsOwner" /> control.
    /// </summary>
    internal class OffspringsOwnerDesigner : CompositeControlDesigner
    {      
        private OffspringsOwnerEditDesigner editDesigner;
        
        private DesignerActionListCollection actionLists;

        /// <summary>
        /// Gets ActionLists.
        /// </summary>        
        public override DesignerActionListCollection ActionLists
        {
            get
            {
                if (null == actionLists)
                {
                    actionLists = new DesignerActionListCollection();
                    actionLists.Add(GetActionList());
                }

                return actionLists;
            }
        }
   
        /// <summary>
        /// Gets a value indicating whether the control is editable.
        /// </summary>
        public virtual bool Editable
        {
            get
            {
                return true;
            }
        }

        /// <summary>
        /// Gets the OffspringsOwner control.
        /// </summary>
        public OffspringsOwner OffspringsOwner
        {
            get
            {
                return ViewControl as OffspringsOwner;
            }
        }

        /// <summary>
        /// Gets the HTML that is used to represent the control at design time.
        /// </summary>
        /// <returns>
        /// The HTML that is used to represent the control at design time.
        /// </returns>
        public override string GetDesignTimeHtml()
        {
            if (OffspringsOwner.Contents == null)
            {
                return GetEmptyDesignTimeHtml();
            }

            return base.GetDesignTimeHtml();
        }

        /// <summary>
        /// chooses which designer is on
        /// </summary>
        /// <param name="regions">
        /// The regions.
        /// </param>
        /// <returns>
        /// the appropriate design time html
        /// </returns>
        public override string GetDesignTimeHtml(DesignerRegionCollection regions)
        {
            if (!Editable)
            {
                return base.GetDesignTimeHtml(regions);
            }

            return editDesigner.GetDesignTimeHtml(regions);
        }

        /// <summary>
        /// Returns the content for an editable region of the design-time view of the associated control.
        /// </summary>
        /// <returns>
        /// The persisted content for the region, if the control designer supports editable regions; otherwise, an empty string ("").
        /// </returns>
        /// <param name="region">The <see cref="T:System.Web.UI.Design.EditableDesignerRegion"/> to get content for.</param>
        public override string GetEditableDesignerRegionContent(EditableDesignerRegion region)
        {
            if (!Editable)
            {
                return base.GetEditableDesignerRegionContent(region);
            }

            return editDesigner.GetEditableDesignerRegionContent(region);
        }

        /// <summary>
        /// Initializes the designer with the specified <see cref="T:System.ComponentModel.IComponent"/> object.
        /// </summary>
        /// <param name="component">
        /// The <see cref="T:System.ComponentModel.IComponent"/>, which is the control associated with this designer.
        /// </param>
        public override void Initialize(IComponent component)
        {
            base.Initialize(component);
            editDesigner = new OffspringsOwnerEditDesigner();
            editDesigner.Initialize(component);
        }

        /// <summary>
        /// Specifies the content for an editable region of the control at design time. 
        /// </summary>
        /// <param name="region">
        /// An editable design region contained within the control.
        /// </param>
        /// <param name="content">
        /// The content to assign for the editable design region.
        /// </param>
        public override void SetEditableDesignerRegionContent(EditableDesignerRegion region, string content)
        {
            if (!Editable)
            {
                base.SetEditableDesignerRegionContent(region, content);
                return;
            }

            editDesigner.SetEditableDesignerRegionContent(region, content);
        }

        private DesignerActionList GetActionList()
        {
            return new OffspringsOwnerActionList(this, OffspringsOwner);
        }
    }
}